#include "math/xtable.h"

NS_MATH_BEGIN

bool xtable::exist(const xstring& name) const
{
	return m_map.find(name)!=m_map.end();
}

template<>
bool xtable::add<XValue>(const xstring& name, XValue& val)
{
	if(exist(name))
	{
		return false;
	}else
	{
		// copy the reference of val
		m_map[name] = val;
		return true;
	}
}

template<>
bool xtable::add<XValue>(const xstring& name, const XValue& val)
{
	if(exist(name))
	{
		return false;
	}else
	{
		// copy the reference of val
		m_map[name] = val.Clone();
		return true;
	}
}

bool xtable::del(const xstring& name)
{
	if(exist(name))
	{
		m_map.erase(name);
		return true;
	}else
	{
		return false;
	}
}

void xtable::clear()
{
	m_map.clear();
}

idtype xtable::size() const
{
	return m_map.size();
}

XValue& xtable::operator[] (const xstring& name)
{
	return m_map[name];
}

vector<xstring> xtable::keys() const
{
	vector<xstring> ret;
	ret.resize(size());
	idtype i=0;
	for(auto iter = m_map.begin(); iter!=m_map.end(); ++iter, ++i)
	{
		ret[i] = iter->first;
	}
	return std::move(ret);
}

NS_MATH_END
